﻿/**************************************************************************
*
* Filename:    MotiCam.cs
*
* Author:      MC Chung
*
* Date:        17/11/11
*
* Description:
*   This class represents a Motic Camera
*
****************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace Autofocus
{
    class MotiCam
    {

        public IntPtr hCameraHandle;
        public Int32 binCount;
        public Int32 m_binningIndex;
        public Int32[] m_pWidth;
        public Int32[] m_pHeight;
        public Int32 m_bitDepth;
        public Int32 m_colorChannel;
        public Single m_fMinExposure;
        public Single m_fMaxExposure;
        public Single m_fExposure;
        public Int32 m_iMinOffset;
        public Int32 m_iMaxOffset;
        public Int32 m_iOffset;
        public Int32 m_iGainCount;
        public Single[] m_pGainList;
        public Int32 m_RedGainIndex;
        public Int32 m_GreenGainIndex;
        public Int32 m_BlueGainIndex;

        public MotiCam(IntPtr CameraHandle)
        {
            this.hCameraHandle = CameraHandle;
            binCount = 0;
            m_binningIndex = 0;
            m_bitDepth = 0;
            m_colorChannel = 0;
            m_fMinExposure = 0;
            m_fMaxExposure = 0;
            m_fExposure = 0;
            m_iMinOffset = 0;
            m_iMaxOffset = 0;
            m_iOffset = 0;
            m_iGainCount = 0;
            m_RedGainIndex = 0;
            m_GreenGainIndex = 0;
            m_BlueGainIndex = 0;
        }

        public Boolean OpenCamera()
        {
            if (Motic.MUCam_openCamera(hCameraHandle))
            {
                // @Binning
                binCount = Motic.MUCam_getBinningCount(hCameraHandle);
                if (binCount <= 0)
                {
                    this.CloseCamera();
                    return false;
                }
                // Get the binning list
                m_pWidth = new Int32[binCount];
                m_pHeight = new Int32[binCount];
                Motic.MUCam_getBinningList(hCameraHandle, m_pWidth, m_pHeight);
                // Select binning 0 as default
                Motic.MUCam_setBinningIndex(hCameraHandle, 0);

                // @BitCount
                if (Motic.MUCam_setBitCount(hCameraHandle, 8))
                {
                    m_bitDepth = 8;
                }
                m_colorChannel = 3;
                Motic.MUCam_Format fmt = Motic.MUCam_getFrameFormat(hCameraHandle);
                if (fmt == Motic.MUCam_Format.MUCAM_FORMAT_MONOCHROME)
                {
                    m_colorChannel = 1;
                }

                // @Exposure
                Motic.MUCam_getExposureRange(hCameraHandle, ref m_fMinExposure, ref m_fMaxExposure);
                Single fval = 600.0f; // set a default exposure
                if (Motic.MUCam_setExposure(hCameraHandle, fval))
                {
                    m_fExposure = fval;
                }

                // @Offset
                Motic.MUCam_getOffsetRange(hCameraHandle, ref m_iMinOffset, ref m_iMaxOffset);
                Int32 val = (m_iMinOffset + m_iMaxOffset) / 2;
                if (Motic.MUCam_setRGBOffset(hCameraHandle, val, val, val))
                {
                    m_iOffset = val;
                }


                // @Gain
                m_iGainCount = Motic.MUCam_getGainCount(hCameraHandle);
                m_pGainList = new Single[m_iGainCount];
                Motic.MUCam_getGainList(hCameraHandle, m_pGainList);
                Motic.MUCam_setRGBGainValue(hCameraHandle, 1.0f, 1.0f, 1.0f, ref m_RedGainIndex, ref m_GreenGainIndex, ref m_BlueGainIndex);

                return true;
            }

            else
                return false;
        }

        public void CloseCamera()
        {
            Motic.MUCam_closeCamera(hCameraHandle);
        }

        public void Release()
        {
            Motic.MUCam_releaseCamera(hCameraHandle);
        }

        public Boolean GetFrame(ref Byte[] buf)
        {
            UInt32 timestep = 0;
            if (Motic.MUCam_getFrame(hCameraHandle, buf, ref timestep))
                return true;
            else
                return false;
        }

        public Boolean setRGBOffset()
        {
            if (Motic.MUCam_setRGBOffset(hCameraHandle, m_iOffset, m_iOffset, m_iOffset))
                return true;
            else
                return false;
        }

        public Boolean setRGBGainIndex()
        {
            if (Motic.MUCam_setRGBGainIndex(hCameraHandle, m_RedGainIndex, m_GreenGainIndex, m_BlueGainIndex))
                return true;
            else
                return false;
        }

        public Boolean setExposure()
        { 
        if (Motic.MUCam_setExposure(hCameraHandle, m_fExposure))
        {
            return true;
        }
        else
            return false;
        }

    }
}
